function DoublyLinkedList() {
    // 创建节点构造函数
    function Node(element) {
        this.element = element;
        this.prev = null;
        this.next = null;
    }

    // 定义属性
    this.length = 0;
    this.head = null;
    this.tail = null;

    // 定义方法
    // 在尾部添加数据
    DoublyLinkedList.prototype.append = function (element) {
        // 根据元素创建节点
        var newNode = new Node(element);

        // 判断链表内容是否为空
        if (this.head === null) {
            this.head = newNode;
            this.tail = newNode;
        } else {
            this.tail.next = newNode;
            newNode.prev = this.tail;
            this.tail = newNode;
        }

        this.length++;
    }

    // 正向遍历
    DoublyLinkedList.prototype.forwardString = function () {
        var current = this.head;
        var forwardStr = '';
        while (current) {
            forwardStr += ',' + current.element;
            current = current.next;
        }        

        return forwardStr.slice(1)
    }

    // 反向遍历
    DoublyLinkedList.prototype.backwardString = function () {
        var current = this.tail;
        var backwardStr = '';

        while(current) {
            backwardStr += ',' + current.element;
            current = current.prev;
        }

        return backwardStr.slice(1)
    }

    // 在任意位置插入数据
    DoublyLinkedList.prototype.insert  = function (position, element) {
        // position位置非法
        if (position < 0 || position > this.length) return false;

        // 不需要考虑是第一个插入节点，如果链表为空，此时position为3，直接返回false，
        // 如果position为0，直接是在第一个位置插入数据,再判断是否为空

        // 创建节点
        var newNode = new Node(element);

        // 判断插入的位置
        if (position === 0) { // 在第一个位置插入
            // 判断链表是否为空
            if (this.length === 0) {
                this.head = newNode;
                this.tail = newNode;
            } else {
                this.head.prev = newNode;
                newNode.next = this.head;
                this.head = newNode;
            }
        } else if (position === this.length) { // 在尾部插入
            this.tail.next = newNode;
            newNode.prev = this.tail;
            this.tail = newNode;
        } else { // 在中间插入
            // 先遍历到position的位置
            var current = this.head;
            var previous = null;
            for(var i = 0;i < position;i++) {
                previous = current;
                current = current.next;
            }

            // 交换节点
            newNode.next = current;
            newNode.prev = previous;
            current.prev = newNode;
            previous.next = newNode;
        }

        this.length++;
        return true;
    }

    // update修改数据
    DoublyLinkedList.prototype.update = function (position,element) {
        if (position < 0 || position >= this.length) return null;

        // 如果是第一个
        if (position === 0) {
            this.head.element = element;
        } else {
            var current = this.head;
            for(var i = 0;i < position;i++) {
                current = current.next;
            }

            current.element = element;
        }
    }

    // indexOf()
    DoublyLinkedList.prototype.indexOf = function (element) {

        var current = this.head;

        for(var i = 0;i < this.length;i++) {
            if (current.element === element) return i;
            else {
                current = current.next;
            }
        }

        return -1;
    }

    // removeAt(position) 从链表特定位置移除一项
    DoublyLinkedList.prototype.removeAt = function (position) {
        if (position < 0 || position >= this.length) return null;

        var current = this.head;
        // 链表内只有一个元素
        if (this.length === 1) {
            this.head = null;
            this.tail = null;
        } else {
            // 删除的是第一个
            if (position === 0) {
                this.head.next.prev =null;
                this.head = this.head.next;
            } else if (position === this.length - 1) { // 删除的是最后一个
                current = this.tail;
                this.tail.prev.next = null;
                this.tail = this.tail.prev;
            } else { // 删除的是中间的
                
                for(var i = 0;i < position;i++) {
                    current = current.next;
                }

                // 删除
                current.next.prev = current.prev;
                current.prev.next = current.next;
            }

        }

        this.length--;
        return current.element;
    }

    // remove(element) 删除指定元素
    DoublyLinkedList.prototype.remove = function (element) {
        var index = this.indexOf(element);
        return this.removeAt(index);
    }

    // isEmpty() 
    DoublyLinkedList.prototype.isEmpty = function () {
        return this.length === 0;
    }

    // size()
    DoublyLinkedList.prototype.size = function () {
        return this.length;
    }
}